package com.itheima.controller.role;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.utils.BeanUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@WebServlet("/role/*")
public class RoleServlet extends BaseServlet {
    /**
     * 点击角色管理显示所有角色信息
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取页面数据
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        //调用业务层查询所有方法
        List<Role> list1 = roleService.list(pageNum, pageSize);
        //把查询结果存入pageInfo中
        PageInfo<Role> pageInfo = new PageInfo<>(list1);
        //把页面数据存入request作用域中
        req.setAttribute("page", pageInfo);
        //跳转到role-list.jsp页面
        req.getRequestDispatcher("/pages/role/role-list.jsp").forward(req, resp);
    }

    /**
     * 新增
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    private void save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //使用工具类封装对象
        Role role = BeanUtil.fillBean(req, Role.class);
        //调用业务层save方法保存对象
        roleService.save(role);
        //显示所有数据
        list(req, resp);
    }

    /**
     * 删除
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    private void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取ids数组
        String ids = req.getParameter("ids");
        //调用业务层方法传递ids
        boolean flag = roleService.deleteByIds(ids);
        //将flag转换为json对象
        String json = mapper.writeValueAsString(flag);
        //将json写出到页面
        resp.getWriter().write(json);
    }

    /**
     * 添加权限名道role表中
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    private void addPermissionToRole(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取role的id
        String id = req.getParameter("id");
        //通过id查询相应role_permission表的对应ID
        List<String> pids = roleService.findById(id);
        //存入到请求域中
        req.setAttribute("pids", pids);
        List<Permission> permissionList = permissionService.findAll();
        //把获取的permission对象存在到请求域
        req.setAttribute("permissionList", permissionList);
        req.setAttribute("roleid", id);
        //跳转到添加权限页面
        req.getRequestDispatcher("/pages/role/role-permission-add.jsp").forward(req, resp);
    }

    /**
     * 给选中的role表中角色对象赋上相应的权限
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    private void savePermission(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取中间表role_permission表中的roleId
        String roleId = req.getParameter("roleId");
        Map<String, String[]> parameterMap = req.getParameterMap();
        String[] pids = parameterMap.get("ids");
        //删除角色对应所有权限
        roleService.deletePermission(roleId);
        //添加权限
        roleService.savePermission(pids, roleId);
        //跳转
        list(req, resp);
    }

}
